<%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <%@ page import="entity.Admin" %>
        <%@ page import="entity.FeeType" %>
            <%@ page import="dao.FeeTypeDao" %>
                <%@ page import="java.util.List" %>
                    <html>

                    <head>
                        <title>费用项目管理</title>
                        <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css"
                            rel="stylesheet">
                        <link href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.8.1/font/bootstrap-icons.css"
                            rel="stylesheet">
                        <style>
                            body {
                                background-color: #f8f9fa;
                                padding-top: 2rem;
                            }

                            .card {
                                box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
                                border: none;
                            }

                            .status-badge {
                                width: 65px;
                            }
                        </style>
                    </head>

                    <body>
                        <% Admin admin=(Admin) session.getAttribute("admin"); if (admin==null) {
                            response.sendRedirect("../adminLogin.jsp"); return; } FeeTypeDao feeTypeDao=new
                            FeeTypeDao(); List<FeeType> feeTypes = feeTypeDao.getAllFeeTypes();
                            %>

                            <div class="container">
                                <div class="row justify-content-center">
                                    <div class="col-md-10">
                                        <div class="d-flex justify-content-between align-items-center mb-4">
                                            <div class="d-flex align-items-center">
                                                <a href="dashboard.jsp" class="btn btn-outline-secondary me-3">
                                                    <i class="bi bi-arrow-left"></i> 返回控制台
                                                </a>
                                                <h2>费用项目管理</h2>
                                            </div>
                                            <div>
                                                <button type="button" class="btn btn-primary me-2"
                                                    data-bs-toggle="modal" data-bs-target="#addFeeTypeModal">
                                                    <i class="bi bi-plus-lg"></i> 添加费用项目
                                                </button>
                                                <a href="${pageContext.request.contextPath}/logout"
                                                    class="btn btn-outline-danger">
                                                    <i class="bi bi-box-arrow-right"></i> 退出登录
                                                </a>
                                            </div>
                                        </div>

                                        <div class="card">
                                            <div class="card-body">
                                                <div class="table-responsive">
                                                    <table class="table table-hover">
                                                        <thead class="table-light">
                                                            <tr>
                                                                <th>费用类型</th>
                                                                <th>单价</th>
                                                                <th>计费单位</th>
                                                                <th>说明</th>
                                                                <th>状态</th>
                                                                <th>操作</th>
                                                            </tr>
                                                        </thead>
                                                        <tbody>
                                                            <% for (FeeType feeType : feeTypes) { %>
                                                                <tr>
                                                                    <td>
                                                                        <%= feeType.getName() %>
                                                                    </td>
                                                                    <td>¥<%= String.format("%.2f", feeType.getPrice())
                                                                            %>
                                                                    </td>
                                                                    <td>
                                                                        <%= feeType.getUnit() %>
                                                                    </td>
                                                                    <td>
                                                                        <%= feeType.getDescription() !=null ?
                                                                            feeType.getDescription() : "" %>
                                                                    </td>
                                                                    <td>
                                                                        <span class="badge <%= feeType.getStatus() == 1 ? 
                                                                                " bg-success" : "bg-danger" %>
                                                                            status-badge"
                                                                            >
                                                                            <%= feeType.getStatus()==1 ? "启用" : "禁用" %>
                                                                        </span>
                                                                    </td>
                                                                    <td>
                                                                        <button class="btn btn-sm btn-outline-primary"
                                                                            onclick="editFeeType(<%= feeType.getId() %>)">
                                                                            <i class="bi bi-pencil"></i>
                                                                        </button>
                                                                        <button
                                                                            class="btn btn-sm btn-outline-danger ms-1"
                                                                            onclick="deleteFeeType(<%= feeType.getId() %>)">
                                                                            <i class="bi bi-trash"></i>
                                                                        </button>
                                                                    </td>
                                                                </tr>
                                                                <% } %>
                                                        </tbody>
                                                    </table>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>

                            <!-- 添加费用项目模态框 -->
                            <div class="modal fade" id="addFeeTypeModal" tabindex="-1">
                                <div class="modal-dialog">
                                    <div class="modal-content">
                                        <div class="modal-header">
                                            <h5 class="modal-title">添加费用项目</h5>
                                            <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
                                        </div>
                                        <form action="${pageContext.request.contextPath}/admin/feeType/add" method="post" accept-charset="UTF-8">

                                        <div class="modal-body">
                                                <div class="mb-3">
                                                    <label class="form-label">费用类型名称</label>
                                                    <input type="text" class="form-control" name="name" required>
                                                </div>
                                                <div class="mb-3">
                                                    <label class="form-label">单价</label>
                                                    <input type="number" class="form-control" name="price" step="0.01"
                                                        min="0" required>
                                                </div>
                                                <div class="mb-3">
                                                    <label class="form-label">计费单位</label>
                                                    <input type="text" class="form-control" name="unit" required>
                                                </div>
                                                <div class="mb-3">
                                                    <label class="form-label">说明</label>
                                                    <textarea class="form-control" name="description"
                                                        rows="3"></textarea>
                                                </div>
                                            </div>
                                            <div class="modal-footer">
                                                <button type="button" class="btn btn-secondary"
                                                    data-bs-dismiss="modal">取消</button>
                                                <button type="submit" class="btn btn-primary">保存</button>
                                            </div>
                                        </form>
                                    </div>
                                </div>
                            </div>

                            <script
                                src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
                            <script>
                                function editFeeType(id) {
                                    window.location.href = '${pageContext.request.contextPath}/admin/editFeeType.jsp?id=' + id;
                                }

                                function deleteFeeType(id) {
                                    if (confirm('确定要删除这个费用项目吗？')) {
                                        window.location.href = '${pageContext.request.contextPath}/admin/feeType/delete?id=' + id;
                                    }
                                }
                            </script>
                    </body>

                    </html>